package things;

import AI.AI;
import Room.Room;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

public class World extends JPanel implements Runnable {

    public int width, height;
    public Graphics2D g2;
    private Thread animator;
    public int x = 0, y = 3, row = 4, col = 4;
    public Player p = new Player(this);
    public Draw d;
    public Room[][] rs;
    public Dimension cellSize;
    private final AI a = new AI(this);

    /**
     * @param width of the world
     * @param height of the world
     */
    public World(int width, int height) {
        this.width = width;
        this.height = height;
        setBackground(Color.white);
        cellSize = new Dimension(width / 4, height / 4);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g2 = (Graphics2D) g;
        antiAliasing();
        rs = new Room[row][col];
        for (int i = 0; i < row; i++) { // create all the rooms
            for (int j = 0; j < col; j++) {
                rs[i][j] = new Room(this, i, j);
            }
        }
        rs[2][2].makePit(false); // make the room at (2,2) a pit, don't highlight the breeze in adjacent rooms.
        rs[1][1].makePit(false);
        rs[2][3].makePit(false);
        rs[0][0].makeGold();
        rs[1][2].makeWumpus();
        d = new Draw(this, Color.black); // create an object so it can draw things
        p.draw(x, y, this);
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    /**
     * The main run method. Any actions you want to happen goes in here.
     */
    public void run() {
        sleep(500);
        a.run();

    }

    /**
     * Telling the thread to start running.
     */
    @Override
    public void addNotify() {
        super.addNotify();
        animator = new Thread(this);
        animator.start();
    }

    /**
     * A shorter sleep method
     * @param miliSeconds How many miliseconds to sleep
     */
    public void sleep(int miliSeconds) {
        try {
            Thread.sleep(miliSeconds);
        } catch (InterruptedException ex) {
            Logger.getLogger(World.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Adds anti-aliasing stuff to the screen so it looks smoother.
     */
    public void antiAliasing() {
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2.setRenderingHints(rh);
    }
}
